package com.lin.controller.user.community;


import com.lin.common.result.PageResult;
import com.lin.common.result.Result;
import com.lin.entity.Comments;
import com.lin.entity.DynamicEntity;
import com.lin.entity.User;
import com.lin.entity.dto.CommentDTO;
import com.lin.entity.dto.DynamicDto;
import com.lin.entity.dto.MusicDTO;
import com.lin.entity.vo.CommentVO;
import com.lin.entity.vo.DynamicEntityVO;
import com.lin.service.CommunityService.CommunityService;
import com.lin.service.CommunityService.IBmsCommentService;
import com.lin.service.CommunityService.IBmsPostService;
import com.lin.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 社区功能
 */
@RestController
@RequestMapping("/community")
@Api(tags = "社区相关接口")
@Slf4j
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    @Autowired
    private IBmsPostService iBmsPostService;

    @Autowired
    private IBmsCommentService bmsCommentService;
    @Autowired
    private PersonService personService;
    /**
     * 发表动态信息 保存到数据库
     * @param
     * @return
     */
    @PostMapping("/post")
    @ApiOperation("新建动态信息到数据库")
    public Result<DynamicEntity> savedynamic(@RequestBody DynamicEntity dynamic){
        log.info("新建动态信息：{}",dynamic);
        dynamic.setDynamicTime(LocalDateTime.now());
        communityService.savedynamic(dynamic);
        return Result.success(dynamic);
    }
    /**
     * 获取10条最新动态信息 返回给前端
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("按分页获取10条动态信息")
    public Result<PageResult> getRandomDynamics(DynamicDto dynamicDto){
        PageResult pageResult = iBmsPostService.getDynamics(dynamicDto);
        return Result.success(pageResult);
    }
    /**
     *获取最新评论 返回给前端
     *
     * @param
     * @return
     */
    @GetMapping("/getcomment")
    @ApiOperation("获取动态信息的评论")
    public Result<List<CommentVO>> getCommentsByTopicID(Integer dynamicid) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(dynamicid);
        return Result.success(lstBmsComment);
    }

    /**
     *添加评论 返回给前端
     *
     * @param
     * @return
     */
    @PostMapping("/addcomment")
    @ApiOperation("添加动态信息的评论")
    public Result<Comments> addComment(@RequestHeader Integer userId,
                                       @RequestBody CommentDTO dto) {
        User user = personService.getUserByUserid(userId);
        Comments comment = bmsCommentService.create(dto, user);
        return Result.success(comment);
    }
}
